package lv.ante.dziesmas.raster;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.renderable.ParameterBlock;

import javax.media.jai.JAI;
import javax.media.jai.RenderedOp;
import javax.swing.JFrame;

/**
 * Intro.java -- objects of this class perform the following steps: 1. reads an
 * image file 2. scales the image dimensions using provided scale factor 3.
 * displays the result
 */
public class Display extends JFrame {
	static {
		System.setProperty("com.sun.media.jai.disableMediaLib", "true");
	}

	public Display(String filename) {
		ParameterBlock pb = new ParameterBlock();
		pb.add(filename);
		RenderedOp inputRO = JAI.create("fileload", pb);
		pb = new ParameterBlock();
		pb.addSource(inputRO);

		// float scale = Float.parseFloat(scaleFactor);
		pb.add(1.0F); // x dimension scale factor
		pb.add(1.0F); // y dimension scale factor
		RenderedOp scaledRO = JAI.create("scale", pb);
		// display result
		getContentPane().add(new Ch6Display(scaledRO));
		pack();
		show();
	}

	public static void main(String[] args) {
		if (args.length != 1) {
			System.err
					.println("Usage: lv.ante.dziesmas.raster.Display <filename>");
		} else {
			final Display dd = new Display(args[0]);

			dd.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
			// what happens when user closes the JFrame.
			WindowListener windowListener = new WindowAdapter() {
				// anonymous WindowAdapter class
				public void windowClosing(WindowEvent w) {
					// Whatever code you want to actually close the JFrame, e.g.
					dd.setVisible(false);
					dd.dispose();
				} // end windowClosing
			};// end anonymous class
			dd.addWindowListener(windowListener);

		}
	}
}
